<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h2>设备监控大屏</h2>
      <div class="time-display">{{ currentTime }}</div>
    </div>
    
    <div class="dashboard-content">
      <!-- 设备状态卡片 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="status-card" shadow="hover">
            <div class="card-content">
              <div class="card-icon online">
                <el-icon><Monitor /></el-icon>
              </div>
              <div class="card-info">
                <div class="card-title">在线设备</div>
                <div class="card-value">{{ onlineCount }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="status-card" shadow="hover">
            <div class="card-content">
              <div class="card-icon offline">
                <el-icon><WarningFilled /></el-icon>
              </div>
              <div class="card-info">
                <div class="card-title">离线设备</div>
                <div class="card-value">{{ offlineCount }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="status-card" shadow="hover">
            <div class="card-content">
              <div class="card-icon warning">
                <el-icon><AlarmClock /></el-icon>
              </div>
              <div class="card-info">
                <div class="card-title">告警设备</div>
                <div class="card-value">{{ warningCount }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="status-card" shadow="hover">
            <div class="card-content">
              <div class="card-icon total">
                <el-icon><Connection /></el-icon>
              </div>
              <div class="card-info">
                <div class="card-title">设备总数</div>
                <div class="card-value">{{ devices.length }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 设备列表 -->
      <el-row :gutter="20" class="dashboard-row">
        <el-col :span="24">
          <el-card class="device-list-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <span>设备状态</span>
              </div>
            </template>
            <el-table :data="devices" style="width: 100%" :stripe="true">
              <el-table-column prop="deviceName" label="设备名称" />
              <el-table-column prop="deviceId" label="设备编号" />
              <el-table-column prop="location" label="位置" />
              <el-table-column prop="lastUpdate" label="最后更新" :formatter="formatDate" />
              <el-table-column label="状态" width="100">
                <template #default="scope">
                  <el-tag :type="scope.row.status === 'online' ? 'success' : 'danger'" size="small">
                    {{ scope.row.status === 'online' ? '在线' : '离线' }}
                  </el-tag>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { Monitor, WarningFilled, AlarmClock, Connection } from '@element-plus/icons-vue'

const currentTime = ref(new Date().toLocaleString())
let timer = null

// 示例设备数据
const devices = ref([
  {
    id: 1,
    deviceName: '油表设备',
    deviceId: '0X0022G5DK000001',
    location: '上海',
    status: 'online',
    lastUpdate: new Date().toISOString()
  }
])

// 计算设备统计
const onlineCount = computed(() => devices.value.filter(d => d.status === 'online').length)
const offlineCount = computed(() => devices.value.filter(d => d.status === 'offline').length)
const warningCount = computed(() => 0) // 暂时固定为0

// 更新时间
const updateTime = () => {
  currentTime.value = new Date().toLocaleString()
}

// 格式化日期
const formatDate = (row, column, cellValue) => {
  if (!cellValue) return '-'
  return new Date(cellValue).toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  // 启动时间更新
  timer = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  // 清理定时器
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
  height: 100%;
  background-color: #f0f2f5;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dashboard-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.time-display {
  font-size: 16px;
  color: #606266;
}

.dashboard-content {
  height: calc(100% - 60px);
}

.dashboard-row {
  margin-top: 20px;
  height: calc(100% - 140px);
}

.status-card {
  background: linear-gradient(to right bottom, #ffffff, #fafafa);
  border: none;
  transition: transform 0.3s;
}

.status-card:hover {
  transform: translateY(-5px);
}

.card-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.card-icon .el-icon {
  font-size: 24px;
  color: white;
}

.card-icon.online {
  background-color: #67C23A;
}

.card-icon.offline {
  background-color: #F56C6C;
}

.card-icon.warning {
  background-color: #E6A23C;
}

.card-icon.total {
  background-color: #409EFF;
}

.card-info {
  flex: 1;
}

.card-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.device-list-card {
  height: 100%;
}

:deep(.el-card__header) {
  padding: 12px 20px;
  border-bottom: 1px solid #EBEEF5;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  font-size: 16px;
  font-weight: 500;
}

:deep(.el-table) {
  background-color: transparent;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
}

:deep(.el-card__body) {
  padding: 0;
}
</style>
